package com.noahplat.person.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.LifeMapper;
import com.noahplat.person.mapper.TasteMapper;
import com.noahplat.person.pojo.Life;
import com.noahplat.person.pojo.Taste;
import com.noahplat.person.service.TasteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: TasteServiceImpl
 * @Description: 口味偏好实现类
 * @Author 李景星
 * @Date 2021/3/8
 * @Version 1.0
 */
@Service
public class TasteServiceImpl implements TasteService {

    @Autowired
    private TasteMapper tasteMapper;
    @Autowired
    private LifeMapper lifeMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取口味偏好
     * @return
     */
    @Override
    public Result getTaste() {
        /*先查redis, 再查数据库*/
        List<Taste> list = new ArrayList<Taste>();
        if(redisUtils.hasKey(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode())){
            list = (List<Taste>)(List)redisUtils.lGet(NoahEnum.TASTEDIR.getCode() + NoahEnum.TASTEKEY.getCode(), 0, -1);
        }else {
            list = tasteMapper.selectAll();
            for (Taste taste : list) {
                redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste);
            }
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }


    /**
     * 添加口味偏好
     * @param taste
     * @return
     */
    @Transactional
    @Override
    public Result addTaste(Taste taste){
        int res = tasteMapper.insertSelective(taste);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改口味偏好
     * @param taste
     * @return
     */
    @Transactional
    @Override
    public Result updateTaste(Taste taste){
        int res = tasteMapper.updateByPrimaryKeySelective(taste);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode())){
            List<Object> list = redisUtils.lGet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), 0, -1);
            for(Object object : list){
                Taste taste1 = (Taste) object;
                if(taste1.getId() == taste.getId()){
                    Long delres = redisUtils.lRemove(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"修改失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(),taste);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"修改失败");
                    }
                    break;
                }
            }
        }else {
            List<Taste> list = tasteMapper.selectAll();
            for (Taste taste1 : list) {
                boolean inres;
                if(taste1.getId() == taste.getId()){
                    inres = redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste);
                }else {
                    inres = redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste1);
                }
                if(!inres){
                    return new Result(false,StatusCode.ERROR,"修改失败");
                }
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除口味偏好
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delTaste(Long id){
        Example example = new Example(Life.class);
        example.createCriteria().andEqualTo("taste",id);
        List<Life> list = lifeMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"正在使用，不能删除");
        }
        int res = tasteMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }

        if(redisUtils.hasKey(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode())){
            List<Object> list1 = redisUtils.lGet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), 0, -1);
            for(Object object : list1){
                Taste taste = (Taste) object;
                if(taste.getId() == id){
                    Long delres = redisUtils.lRemove(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除失败");
                    }
                    break;
                }
            }
        }else {
            List<Taste> list1 = tasteMapper.selectAll();
            for (Taste taste : list1) {
                if(taste.getId() == id){
                    continue;
                }
                boolean inres = redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste);
                if(!inres){
                    return new Result(false,StatusCode.ERROR,"删除失败");
                }
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 分页获取口味偏好
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAdminTaste(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Taste> list = tasteMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Taste> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 获取口味偏好详细信息
     * @param id
     * @return
     */
    @Override
    public Result getTasteById(Long id){
        List<Taste> list = new ArrayList<Taste>();
        if(redisUtils.hasKey(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode())){
            list = (List<Taste>)(List)redisUtils.lGet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), 0, -1);
        }else {
            list = tasteMapper.selectAll();
            for (Taste taste : list) {
                redisUtils.lSet(NoahEnum.TASTEDIR.getCode()+NoahEnum.TASTEKEY.getCode(), taste);

            }
        }
        for(Taste taste : list){
            if(taste.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",taste);
            }
        }
        return new Result(false,StatusCode.ERROR,"暂无数据");
    }

}
